package com.sh.test;

import com.sh.promise.IPromise;
import com.sh.promise.Promise;
import com.sh.promise.PromiseExecutor;
import com.sh.promise.PromiseHandler;
import com.sh.promise.then.OnCatchedExecutor;
import com.sh.promise.then.OnCompleteListener;
import com.sh.promise.then.OnFulfilledExecutor;

/**
 * @Date 13:04 2023/3/9
 **/
public class Demo01 {
    public static void main(String[] args){
        int a = 3 ;
        IPromise promise1 = new Promise.Builder().externalInput(a).promiseHandler(executor -> {
            Integer externalInput = (Integer) executor.getExternalInput();
            throw new Exception("报错了");
            //return externalInput * 3;
        }).build();
        System.out.println(promise1.get());
        promise1.addListener((resolvedData, e) -> {
            System.out.println(resolvedData);
            System.out.println(e.getMessage());
        });
        promise1.pCatch(new OnCatchedExecutor() {
            @Override
            public Object onCatched(Throwable catchReason) throws Exception {
                System.out.println("onCatched : "+catchReason.getMessage());
                return "catch中返回的结果";
            }
        }).then(new OnFulfilledExecutor() {
            @Override
            public Object onFulfilled(Object resolvedData) throws Exception {
                System.out.println("then : " + resolvedData);
                return "回调结果";
            }
        }).addListener(new OnCompleteListener() {
            @Override
            public void listen(Object resolvedData, Throwable e) {
                System.out.println("addListener : " + resolvedData);
            }
        });
    }
}
